<?php
include_once(dirname(__FILE__) . "/../../../include/global.php");
function getdevice($id,$hostname,$snmp_community){
	//$host = db_fetch_row('SELECT id,hostname,snmp_community FROM host WHERE status !=1 and id = '.$id.'');
	$model = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "physicalname"');
	$rvalue = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "rvalue"');
	$thold = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "thold"');
	$name = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "getname"');
	$hwoid = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "hardware"');
	$fwoid = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "firmware"');
	$swoid = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "software"');
	$seoid = db_fetch_cell('SELECT oid_get FROM plugin_nms_oid WHERE descrip = "serial"');
	// clear old data
	db_execute('UPDATE plugin_nms_translog
				SET trans_serial ="",
					trans_name="",
					rvalue = "Unknow",
					thold =0
				WHERE host_id='.$id.' and trans_date = curdate()');
	// snmp walk get devices information
	$snmp2 = snmp2_real_walk($hostname,$snmp_community,$model);
	foreach($snmp2 as $x=>$x_value){
		$oid = str_replace("SNMPv2-SMI::mib-2.47.1.1.1.1.7.",'',$x);
		$model = trim(str_replace("STRING: ",'',$x_value),'""');
		if($model != ""){
			$get = explode(" ",$model);
			if($get[0] == 'module' & count($get) == 2){
				// get module name
				$mname =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$name.'.'.$oid)),'""');
				// get hardware
				$hw =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$hwoid.'.'.$oid)),'""');
				//get firmware
				$fw =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$fwoid.'.'.$oid)),'""');
				//get software
				$sw =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$swoid.'.'.$oid)),'""');
				//get serial
				$se =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$seoid.'.'.$oid)),'""');
				$card = db_fetch_cell('SELECT module_serial FROM plugin_nms_module WHERE module_serial = "'.$se.'"');
				if($se!=''){
					if($card !=''){
						$current = db_fetch_cell('SELECT count(*) FROM plugin_nms_modulelog WHERE module_serial = "'.$se.'" and module_date = curdate()');
						if($current < 1){
							db_execute('insert into plugin_nms_modulelog(module_serial,module_date,host_id,module_num,software)
											value ("'.$se.'",curdate(),'.$id.','.$get[1].',"'.$sw.'")');
						}
						else{
							db_execute('update plugin_nms_modulelog
											set host_id = '.$id.',
												module_num = '.$get[1].',
												software = "'.$sw.'"
											where module_serial = "'.$se.'" and module_date = curdate()');
						}

					}
					else{
						// insert new card
						db_execute('insert into plugin_nms_module(module_serial,module_name,hardware,firmware)
									value ("'.$se.'","'.$mname.'","'.$hw.'","'.$fw.'")');
						// insert frist module log
						db_execute('insert into plugin_nms_modulelog(module_serial,module_date,host_id,module_num,software)
									value ("'.$se.'",curdate(),'.$id.','.$get[1].',"'.$sw.'")');
					}
				}
			}

			else{
				$sub = str_replace("switching engine ",'',$model);
				$sub = str_replace("msfc ",'',$sub);
				$exp = explode(" ",$sub);
				if($exp[0] == 'sub-module' and count($exp) == 3){
					//get serail of sub-module
					$se =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$seoid.'.'.$oid)),'""');
					if($se!=''){
						$cursub = db_fetch_cell('SELECT count(*) FROM plugin_nms_submodule WHERE sub_serial = "'.$se.'"');
						if($cursub <1 and $se !=''){
							//get name of sub-module
							$sname = trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$name.'.'.$oid)),'""');
							//get hardware of sub-module
							$hw =  trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$hwoid.'.'.$oid)),'""');
							$ms = db_fetch_cell('SELECT module_serial FROM plugin_nms_modulelog where module_num = '.$exp[2].' and module_date = curdate() and host_id = '.$id.'');
							// store into database
							db_execute('insert into plugin_nms_submodule(sub_serial,module_serial,sub_name,hardware) value ("'.$se.'","'.$ms.'","'.$sname.'","'.$hw.'")');
						}
					}
				}
				else{
					$tran = explode(" ",$model);
					if(count($tran)==1 and strlen($tran[0])<7){
						$portcurent  = db_fetch_cell('Select count(*) from plugin_nms_translog where trans_date = curdate() and host_id = '.$id.' and port_name = "'.$tran[0].'"');
						$getport = db_fetch_row('Select interface_id,interface_alias,nms_status,nms_pause from plugin_nms_interface where interface_name ="'.$tran[0].'" and host_id ='.$id.'');
						$group = db_fetch_cell('Select interface_group_id From plugin_nms_interfacegroup Where interface_id = '.$getport['interface_id'].'');
						if($portcurent <1){
							db_execute('insert into plugin_nms_translog(port_name,trans_date,port_alias,port_status,port_pause,host_id,interface_group_id)
											value ("'.$tran[0].'",curdate(),"'.$getport['interface_alias'].'","'.$getport['nms_status'].'","'.$getport['nms_pause'].'",'.$id.','.$group.')');
						}
						else{
							db_execute('update plugin_nms_translog
												Set port_alias = "'.$getport['interface_alias'].'",
													port_status = "'.$getport['nms_status'].'",
													port_pause = '.$getport['nms_pause'].',
													interface_group_id ='.$group.'
												Where port_name = "'.$tran[0].'" and
														host_id = '.$id.' and
														trans_date = curdate()');
						}
					}
					else{
						if($tran[0] == "Transceiver" & count($tran) == 2){
							$se = trim(str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$seoid.'.'.$oid)),'""');
							//$curtrans = db_fetch_cell('SELECT count(*) FROM plugin_nms_translog WHERE trans_serial = "'.$se.'" and trans_date = curdate()');
							$num = substr($tran[1],2,1);
							$mse = db_fetch_cell('SELECT module_serial FROM plugin_nms_modulelog WHERE module_date = curdate() and module_num = '.$num.' and host_id ='.$id.'' );
							// get status port
							//insert new transceiver
							$type = str_replace("STRING: ",'',snmp2_get($hostname,$snmp_community,$name.'.'.$oid));
							if( strpos($type,'XFP') != false or strpos($type,'SFP') != false){
								$type = trim($type,'""');
							}
							else{
								$type =  'Unknown';
							}
							// insert trans log
							db_execute('update plugin_nms_translog
											set trans_serial ="'.$se.'",
													module_serial ="'.$mse.'",
													trans_name = "'.$type.'"
										Where host_id = '.$id.' and port_name = "'.$tran[1].'" and trans_date = curdate()');
						}
						else{
							if($tran[1] == 'Receive'){
								//get recive vaule
								$getvalue = snmp2_get($hostname,$snmp_community,$rvalue.'.'.$oid);
								$getvalue = explode(" ",$getvalue);
								$getrvalue = $getvalue[1]/10;
								// get threshold
								$getthold=0;
								for($i=1;$i<=4;$i++){
									$temp =snmp2_get($hostname,$snmp_community,$thold.'.'.$oid.'.'.$i);
									$temp = explode(" ",$temp);
									if($temp[1] == '1'){
										$getthold = $i;
									}
								}
								if($getrvalue !=''){
									db_execute('UPDATE plugin_nms_translog SET rvalue = "'.$getrvalue.'",thold='.$getthold.'
										WHERE host_id = '.$id.' and port_name = "'.$tran[0].'" and trans_date = curdate()');
								}
							}
						}
					}
				}
			}
		}
	}
}